import { defineStore, acceptHMRUpdate } from "pinia";
import { invokeAPi, invokeAsyncAPi, reportInfoApi } from "@/xz-fn";
import { getLocalStorage } from '@/utils/storage';
export const settingAppStore = defineStore({
  id: "setting",
  state: () => ({
    enableSltWordFy: false,
    hotKeyInfo_0: {},
    hotKeyInfo_1: {}
  }),
  getters: {
    screenShotKey(){
      if(this.hotKeyInfo_1?.id){
        return this.hotKeyInfo_1.keyStr
      }
    },
    sltWordKey(){
      if(this.hotKeyInfo_0?.id){
        return this.hotKeyInfo_0.keyStr
      }
    }
  },
  actions: {
    // 设置项变更
    change(data: { enableSltWordFy?: boolean, hotKeyInfo_0?: any, hotKeyInfo_1?: any }){
      const { enableSltWordFy, hotKeyInfo_0, hotKeyInfo_1 } = data
      if(typeof enableSltWordFy !== 'undefined'){
        this.enableSltWordFy = enableSltWordFy
      }
      if(typeof hotKeyInfo_0 !== 'undefined'){
        this.hotKeyInfo_0 = hotKeyInfo_0
      }
      if(typeof hotKeyInfo_1 !== 'undefined'){
        this.hotKeyInfo_1 = hotKeyInfo_1
      }
    },
    // 获取-启用/关闭划词翻译
    getEnableSltWordFy(){
      invokeAsyncAPi("fq_HasOpenMarkTextFanYi")
        .then((isEnable: unknown) => {
          this.enableSltWordFy = isEnable as boolean;
        })
    },
    // 设置-启用/关闭划词翻译
    setEnableSltWordFy(val: string | number | boolean = !this.enableSltWordFy) {
      if(val){
        reportInfoApi(42);
        invokeAPi("fq_OpenMarkTextFanYi");
      }else{
        reportInfoApi(43);
        invokeAPi("fq_CloseMarkTextFanYi");
      }
      this.enableSltWordFy = val as boolean;
      invokeAPi("fq_NotifyWebMessageEx", "", JSON.stringify({
        action: "setting-change",
        data: { enableSltWordFy: val } 
      }))
    },
    // 获取快捷
    getHotKeyInfo(){
      const keys = ["hotKeyInfo_0", "hotKeyInfo_1"];
      keys.map(k => {
        const info = getLocalStorage(k);
        this[k] = info
      })
    }
  }
});
if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(settingAppStore, import.meta.hot));
}
